package com.fingard.dsp.bank.directbank.cncbi01;

import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2018/11/12.
 */
public class CNCBI01SinglePay extends CNCBI01Base implements ISinglePay {
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        try {
            if("190801".equals(p_req1908.reqHeader.commandCode)){
                detail = payBySameBank(p_req1908);
            }else {
                detail = payByidiffBank(p_req1908);
            }
        }catch (Exception ex){
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeRetDetail(detail);
        return retAts;
    }

    private RetAtsSglDetail payBySameBank(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail detail = new RetAtsSglDetail();
        XmlTextWriter xmlWriter = new XmlTextWriter();
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"GBK\"?>");
        XElement stream = new XElement("stream","");
        stream.addChild(new XElement("action","DLINETRN"));
        stream.addChild(new XElement("userName",bankFront.loginName));
//        <clientID></clientID><!--客户流水号 char(20)--
        stream.addChild(new XElement("clientID",p_req1908.singleDetail.askNumber));
//        <preFlg></preFlg><!--预约支付标志 0：非预约交易；1：预约交易 char(1)-->
        stream.addChild(new XElement("preFlg","0"));
//        <preDate></preDate><!--延期支付日期char(8)　格式YYYYMMDD ，预约时非空-->
        stream.addChild(new XElement("preDate",""));
//        <preTime></preTime><!--延期支付时间char(6)  格式hhmmss，只限100000，120000，140000，160000 四个时间点，预约时非空-->
        stream.addChild(new XElement("preTime",""));
//        <payAccountNo></payAccountNo><!--付款账号 char(19)-->
        stream.addChild(new XElement("payAccountNo",p_req1908.singleDetail.payAct));
//        <recAccountNo></recAccountNo><!--收款账号char(19)-->
        stream.addChild(new XElement("recAccountNo",p_req1908.singleDetail.recAct));
//        <tranAmount></tranAmount><!--金额 decimal(15,2)-->
        stream.addChild(new XElement("tranAmount",p_req1908.singleDetail.payAmount));
//        <abstract></abstract><!--摘要，可空 varchar(102)-->
        stream.addChild(new XElement("abstract",p_req1908.singleDetail.purpose));
//        <memo></memo><!--备注 varchar(60) 可空-->
        stream.addChild(new XElement("memo",p_req1908.singleDetail.purpose));
//        <cryType></cryType><!--币种char(2)，可空，当收、付款账号为信银国际账号时非空-->
        stream.addChild(new XElement("cryType",convertCurToBank(p_req1908.singleDetail.payCur)));
        nodeAnalysis(p_req1908.singleDetail.srcXElement,stream,getCharset());
        xmlWriter.writeXElement(stream);
        String[] bankResult = sendToBank(xmlWriter.toXmlString());
        if(bankResult[0].length()==0){
            StringBuilder tmpBkMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpBkMsg);
            if(tmpBkReader!=null){
                detail.respCode = "0";
                detail.respInfo = tmpBkReader.getFirstText("/stream/statusText");
                detail.transState = tmpBkReader.getFirstText("/stream/status");
                detail.payInfoCode = detail.transState;
                detail.reqSeqID = p_req1908.singleDetail.askNumber;
            }else {
                detail.respCode ="-1";
                detail.respInfo = tmpBkMsg.toString();
            }
        }else if (bankResult[0].equals(ConnState.FAIL)) {
            detail.respCode = "0";
            detail.respInfo = bankResult[1];
            detail.reqSeqID = p_req1908.singleDetail.askNumber;
            detail.transState = PaymentState.FAIL_Payment;
            detail.payInfoCode = bankResult[0];
        } else{
            detail.respCode ="-1";
            detail.respInfo = bankResult[1];
        }
        return detail;
    }

    private RetAtsSglDetail payByidiffBank(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail detail = new RetAtsSglDetail();
        XmlTextWriter xmlWriter = new XmlTextWriter();
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"GBK\"?>");
        XElement stream = new XElement("stream","");
//        <userName></userName><!--登录名 varchar(30)-->
        stream.addChild(new XElement("action","DLCBITRN"));
//        <clientID></clientID><!--客户流水号 varchar (20)-->
        stream.addChild(new XElement("userName",bankFront.loginName));
//        <preFlg></preFlg><!--预约支付标志char(1)，0：非预约交易；1：预约交易-->
        stream.addChild(new XElement("preFlg","0"));
//        <preDate></preDate><!--延期支付日期char(8),格式：YYYYMMDD，预约时非空-->
        stream.addChild(new XElement("preDate",""));
//        <preTime></preTime><!--延期支付时间char(6),格式：hhmmss，只限100000，120000，140000，160000四个时间点，预约时非空；-->
        stream.addChild(new XElement("preTime",""));
//        <tranType></tranType><!--交易方式char(1)，1-行内转账；2-即日支付；3-海外汇款；4-普通支付-->
        stream.addChild(new XElement("tranType",""));// TODO: 2018/11/12
//        <accountNo></accountNo><!--付款账号char(34) -->
        stream.addChild(new XElement("accountNo",p_req1908.singleDetail.payAct));
//        <payCryType></payCryType><!--付款账户币种char(2) -->
        stream.addChild(new XElement("payCryType",convertCurToBank(p_req1908.singleDetail.recCur)));
//        <rcvAcc></rcvAcc><!--收款账号char(34), 交易方式为行内转账时，需校验长度是12位-->
        stream.addChild(new XElement("rcvAcc",p_req1908.singleDetail.recAct));
//        <rcvNm></rcvNm><!--收款人账户名称char(35), 交易方式为行内转账时，送空；交易方式非行内转账时，必输 -->
        stream.addChild(new XElement("rcvNm",p_req1908.singleDetail.recName));
//        <rcvCryType></rcvCryType><!--收款账户币种char(2) ,交易方式为行内转账时，必输；交易方式为即日支付和海外汇款和普通支付时，送空；-->
        stream.addChild(new XElement("rcvCryType",convertCurToBank(p_req1908.singleDetail.recCur)));
//        <rcvBankNo></rcvBankNo><!--收款账号行号char(10), 交易方式为行内转时，必输；交易方式为即日支付和海外汇款和普通支付时，送空；交易方式为行内转账时，数据字典：CNCBIHK-信银国际香港、CNCBISG-信银国际新加坡 -->
        stream.addChild(new XElement("rcvBankNo",""));
//        <tranAmt></tranAmt><!--交易金额 decimal（15,2）-->
        stream.addChild(new XElement("tranAmt",p_req1908.singleDetail.payAmount));
//        <cryType></cryType><!--交易币种char(2),
        stream.addChild(new XElement("cryType",convertCurToBank(p_req1908.singleDetail.payCur)));
        nodeAnalysis(p_req1908.singleDetail.srcXElement,stream,getCharset());
        xmlWriter.writeXElement(stream);
        String[] bankResult = sendToBank(xmlWriter.toXmlString());
        if(bankResult[0].length()==0){
            StringBuilder tmpBkMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpBkMsg);
            if(tmpBkReader!=null){
                detail.respCode = "0";
                detail.respInfo = tmpBkReader.getFirstText("/stream/statusText");
                detail.transState = tmpBkReader.getFirstText("/stream/status");
                detail.payInfoCode = detail.transState;
                detail.reqSeqID = p_req1908.singleDetail.askNumber;
            }else {
                detail.respCode ="-1";
                detail.respInfo = tmpBkMsg.toString();
            }
        }else if (bankResult[0].equals(ConnState.FAIL)) {
            detail.respCode = "0";
            detail.respInfo = bankResult[1];
            detail.reqSeqID = p_req1908.singleDetail.askNumber;
            detail.transState = PaymentState.FAIL_Payment;
            detail.payInfoCode = bankResult[0];
        } else{
            detail.respCode ="-1";
            detail.respInfo = bankResult[1];
        }
        return detail;
    }



    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        try{
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CNCBI01Header header =new CNCBI01Header();
            header.action = "DLCIDSTT";
            writeCommonHeader(xmlWriter,header);
//            <clientID></clientID><!--客户流水号char(20)-->
            xmlWriter.writeElementString("clientID",p_req1944.askNumber);
//            <type></type><!--
            xmlWriter.writeElementString("type","");
            xmlWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlWriter.toXmlString());
            if(bankResult[0].length()==0){
                StringBuilder tmpBkMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpBkMsg);
                if(tmpBkReader!=null){
                    String returnCode = tmpBkReader.getFirstText("/stream/status");
                    String returnMsg = tmpBkReader.getFirstText("/stream/statusText");
                    if("AAAAAAA".equals(returnCode)) {
                        detail.respCode = "0";
                        detail.respInfo = tmpBkReader.getFirstText("/stream/list/row/statusText");
                        detail.transState = tmpBkReader.getFirstText("/stream/list/row/status");
                        detail.payInfoCode = detail.transState;
                        detail.reqSeqID = p_req1944.askNumber;
                    }else{
                        detail.respCode ="-1";
                        detail.respInfo = returnCode+" + "+returnMsg;
                    }
                }else {
                    detail.respCode ="-1";
                    detail.respInfo = tmpBkMsg.toString();
                }
            }else{
                detail.respCode ="-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception ex) {
            ex.printStackTrace();
            detail.respCode ="-1";
            detail.respInfo =ex.getMessage();
        }
        retAts.writeRetDetail(detail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
